#ifndef WXSITEM_H
#define WXSITEM_H

#include "wxsevents.h"
#include "wxsiteminfo.h"
#include "wxsitemfactory.h"
#include "wxsbaseproperties.h"
#include "wxspredefinedids.h"
#include "wxscodegenerator.h"
#include "wxsflags.h"
#include "../properties/wxsproperties.h"
#include "../wxsresourcetree.h"
#include <stdarg.h>
#include <prep.h>

class wxsItemResData;

class wxsParent;
class wxsTool;
class wxWindow;
class wxsItem: public wxsPropertyContainer, public wxsCodeGenerator {
  public:
    wxsItem( wxsItemResData* Data, const wxsItemInfo* Info, long PropertiesFlags, const wxsEventDesc* Events, const wxsStyleSet* StyleSet );
    virtual ~wxsItem();
    inline const wxsItemInfo& GetInfo() { return *m_Info; }
    inline wxsEvents& GetEvents() { return m_Events; }
    bool IsRootItem();
    inline bool IsPointer() { return OnIsPointer(); }
    inline wxString GetVarName() { return IsRootItem() ? _T( "this" ) : m_BaseProperties.m_VarName; }
    void SetVarName( const wxString& NewName );
    wxString GetCreatePrefix( wxsCodingLang Language );
    wxString GetAccessPrefix( wxsCodingLang Language );
    wxString GetIdName();
    inline void SetIdName( const wxString& NewIdName ) { m_BaseProperties.m_IdName = NewIdName; }
    inline bool GetIsMember() { return IsPointer() ? m_BaseProperties.m_IsMember : true; }
    inline void SetIsMember( bool NewIsMember ) { m_BaseProperties.m_IsMember = NewIsMember; }
    inline wxsParent* GetParent() { return m_Parent; }
    inline wxsItemResData* GetResourceData() { return m_ResourceData; }
    inline wxsItemType GetType() { return GetInfo().Type; }
    virtual const wxString& GetClassName() { return GetInfo().ClassName; }
    wxString GetUserClass();
    inline void SetUserClass( const wxString& Subclass ) { m_BaseProperties.m_Subclass = Subclass; }
    void EnumItemProperties( long Flags );
    wxObject* BuildPreview( wxWindow* Parent, long PreviewFlags );
    inline bool XmlRead( TiXmlElement* Element, bool IsXRC, bool IsExtra ) { return OnXmlRead( Element, IsXRC, IsExtra ); }
    inline bool XmlWrite( TiXmlElement* Element, bool IsXRC, bool IsExtra ) { return OnXmlWrite( Element, IsXRC, IsExtra ); }
    void BuildItemTree( wxsResourceTree* Tree, wxsResourceItemId Parent, int Position = -1 );
    inline wxsResourceItemId GetLastTreeItemId() { return m_LastTreeId; }
    virtual wxsParent* ConvertToParent() { return 0; }
    virtual wxsTool* ConvertToTool() { return 0; }
    virtual wxsBaseProperties* GetBaseProps() { return &m_BaseProperties; }
    inline wxObject* GetLastPreview() { return m_LastPreview; }
    inline bool GetIsSelected() { return m_IsSelected; }
    inline void SetIsSelected( bool NewSelected ) { m_IsSelected = NewSelected; }
    inline bool GetIsExpanded() { return m_IsExpanded; }
    inline void SetIsExpanded( bool NewExpanded ) { m_IsExpanded = NewExpanded; }
    void ClearSelection();
    inline wxWindowID GetId() { return wxsPredefinedIDs::Value( m_BaseProperties.m_IdName ); }
    inline bool MouseClick( wxWindow* Preview, int PosX, int PosY ) { return OnMouseClick( Preview, PosX, PosY ); }
    inline bool MouseDClick( wxWindow* Preview, int PosX, int PosY ) { return OnMouseDClick( Preview, PosX, PosY ); }
    inline bool MouseRightClick( wxWindow* Preview, int PosX, int PosY ) { return OnMouseRightClick( Preview, PosX, PosY ); }
    inline bool CanAddToParent( wxsParent* Parent, bool ShowMessage ) { return OnCanAddToParent( Parent, ShowMessage ); }
    inline bool PopupMenu( long Id ) { return OnPopup( Id ); }  protected:
    wxString Codef( wxsCoderContext* Context, const wxChar* Fmt, ... );
    void Codef( const wxChar* Fmt, ... );
    virtual void OnBuildDeclarationsCode();
    virtual void OnBuildEventsConnectingCode();
    virtual void OnBuildIdCode();
    virtual void OnBuildXRCFetchingCode();
    void BuildSetupWindowCode();
    virtual long OnGetPropertiesFlags();
    virtual void OnEnumItemProperties( long Flags ) = 0;
    virtual void OnAddItemQPP( M_Code_unused wxsAdvQPP* QPP ) {}
    virtual wxObject* OnBuildPreview( wxWindow* Parent, long PreviewFlags ) = 0;
    virtual bool OnXmlRead( TiXmlElement* Element, bool IsXRC, bool IsExtra );
    virtual bool OnXmlWrite( TiXmlElement* Element, bool IsXRC, bool IsExtra );
    virtual void OnAddExtraProperties( wxsPropertyGridManager* Grid );
    virtual void OnExtraPropertyChanged( wxsPropertyGridManager* Grid, wxPGId Id );
    virtual bool OnMouseClick( M_Code_unused wxWindow* Preview, M_Code_unused int PosX, M_Code_unused int PosY ) { return false; }
    virtual bool OnMouseDClick( wxWindow* Preview, int PosX, int PosY );
    virtual bool OnMouseRightClick( wxWindow* Preview, int PosX, int PosY );
    virtual void OnPreparePopup( wxMenu* Menu );
    virtual bool OnPopup( long Id );
    void ShowPopup( wxMenu* Menu );
    virtual bool OnCodefExtension( M_Code_unused wxsCodingLang Language, M_Code_unused wxString& Result,
                                   M_Code_unused const wxChar* &FmtChar, M_Code_unused va_list ap ) { return false; }
    virtual bool OnIsPointer();
    virtual bool OnCanAddToParent( M_Code_unused wxsParent* Parent, M_Code_unused bool ShowMessage ) { return true; }
    virtual wxString OnGetTreeLabel( int& Image );
    inline wxPoint Pos( wxWindow* Parent ) { return GetBaseProps()->m_Position.GetPosition( Parent ); }
    wxSize Size( wxWindow* Parent );
    inline long Style() { return m_BaseProperties.m_StyleSet ? m_BaseProperties.m_StyleSet->GetWxStyle( m_BaseProperties.m_StyleBits, false ) : 0; }
    wxWindow* SetupWindow( wxWindow* Window, long Flags );  private:
    virtual void OnEnumProperties( long Flags );
    virtual wxsQuickPropsPanel* OnCreateQuickProperties( wxWindow* Parent );
    virtual void OnPropertyChanged();
    virtual void OnSubPropertyChanged( wxsPropertyContainer* );
    void Codef( wxsCoderContext* Context, const wxChar* Fmt, wxString& Result, va_list ap );
    void UpdateTreeLabel();
    virtual void OnUpdateFlags( long& Flags );    const wxsItemInfo* m_Info;
    wxsEvents m_Events;
    wxsParent* m_Parent;
    wxsItemResData* m_ResourceData;
    wxsBaseProperties m_BaseProperties;
    long m_PropertiesFlags;
    wxObject* m_LastPreview;
    bool m_IsSelected;
    bool m_IsExpanded;
    wxsResourceItemId m_LastTreeId;
    friend class wxsParent;
};
#endif
